<?php
namespace App\Http\Controllers\AdminApi;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\FieldTable;
use App\Model\InviteTeamTable;
use App\Model\OrderTable;
use App\Model\StorefrontTable;
use App\Model\TeamTable;
use App\Model\TimeBlockTable;
use App\Model\UserLevelSetTable;
use App\Model\UserTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
use EasyWeChat\Foundation\Application;

//订单
class OrderController extends ApiController
{


    /**
     * @api {get} /adminapi/order/list  订单列表
     * @apiGroup Admin-Order
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     * @apiParam {Int} order_id 订单编号
     * @apiParam {Int} store_id 店面
     * @apiParam {Int} status 订单状态 0全部  1已订场 2已匹配 3待评价
     * @apiParam {Int} order_start_time 起始时间
     * @apiParam {Int} order_end_time 结束时间
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "order_id": "1",
     * "main_team": "1",
     * "slave_team_name": "1",
     * "time": "1970-01-01 08:00-08:00 周四",
     * "address": {
     * "storefront_name": "汉阳",
     * "field_id": "1",
     * "field_type": "1",
     * "storefront_id": "1",
     * "number": "1"
     * },
     * "dateline": "1970-01-01 08:00",
     * "pay_type": "1",
     * "status": "1"
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getList()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);

        $order_id = \Request::get('order_id'); //订单编号
        $store_id = \Request::get('store_id'); //店面
        $status = \Request::get('status'); //订单状态 0全部  1已订场 2已匹配 3待评价
        $order_start_time = \Request::get('order_start_time'); //起始时间
        $order_end_time = \Request::get('order_end_time'); //结束时间

        $param = [];
        $where = 'pay_type>=2';

        if ($order_id) {
            $where .= " and order_id=:order_id";
            $param['order_id'] = $order_id;
        }

        if ($store_id) {
            $where .= " and storefront_id=:storefront_id";
            $param['storefront_id'] = $store_id;
        }

        if ($status) {
            if ($status == 1) {
                $where .= " and order_type = 3 and pay_type = 2 and status = 2 and day_time > ".time();
            }
            if ($status == 2) {
                $where .= " and order_type in(1,2) and pay_type =2 and status = 2";
            }
            if ($status == 3) {
                $where .= " and pay_type = 2 and status = 2 and is_comment = 2";
            }
        }

        if ($order_start_time) {
            $order_start_time = strtotime($order_start_time);
            if ($order_end_time) {
                $order_end_time = strtotime($order_end_time);
                $where .= "  and(day_time>=:order_start_time and day_time<=:order_end_time)";
                $param['order_start_time'] = $order_start_time;
                $param['order_end_time'] = $order_end_time;
            } else {
                $where .= " and( day_time>=:order_start_time)";
                $param['order_start_time'] = $order_start_time;
            }
        }

        $count = \DB::select("select count(*) as c  from `order` WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from `order` WHERE {$where} ORDER BY order_id DESC limit :offset,:limit", $param);

        $list = [];
        foreach ($res as $v) {

            $main_team_name = '';
            $slave_team_name = '';

            if ($v->order_type != 3) {
                $invite_res = InviteTeamTable::ORM()->where('id', $v->object_id)->first();
                $main_team_name = TeamTable::getTeamInfo($invite_res->team_id)->name;
                $slave_team_name = TeamTable::getTeamInfo($invite_res->join_team_id)->name;
            }else{
                $user=UserTable::ORM()->where('id',$v->uid)->first();
                $main_team_name=$user->nickname;  //用户微信名称
            }



            $status=OrderTable::status($v->order_type, $v->order_type, $v->pay_type, $v->is_comment, $v->status, strtotime(date('Y-m-d ', $v->day_time) . date('H:i:s', $v->end_time)));
            $list[] = [
                'order_type'=>$v->order_type,
                'order_id' => $v->order_id,
                'main_team' => $main_team_name,
                'slave_team_name' => $slave_team_name,
                'time' => date('Y-m-d ', $v->day_time) . date('H:i', $v->start_time) . '-' . date('H:i', $v->end_time) . ' 周' . Str::getWeek($v->day_time),
                'address' => FieldTable::getFieldInfo($v->field_id),
                'dateline' => date('Y-m-d H:i', $v->dateline),
                'pay_type' => $v->pay_type,  //支付状态 1 未支付  2已支付 3已退款'
                'status' => $status,  // 翻译状态    // $v->status, //1 匹配中  2已匹配 3已取消
                'pay_type_text' => ConstTable::PAY_TYPE_TEXT[$v->pay_type],
                'status_text' => ConstTable::ORDER_STATUS[$status],
            ];

        }
        $data = [
            'count' => $count,
            'list' => $list,
            'page' => $page,
        ];
        return JsonRespose::success($data);
    }

    /**
     * @api {get} /adminapi/order/info  订单详情
     * @apiGroup Admin-Order
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} order_id 订单编号
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "order_type": "2",
     * "order_id": "1",
     * "storefront_name": "汉阳",
     * "time": "1970-01-01 08:00-08:00 周四",
     * "address": {
     * "storefront_name": "汉阳",
     * "field_id": "1",
     * "field_type": "1",
     * "storefront_id": "1",
     * "number": "1"
     * },
     * "dateline": "1970-01-01 08:00",
     * "status": "1",
     * "price": "1.00",
     * "pay_type": "1",
     * "field_price": "",
     * "man_team_res": {
     * "id": "1",
     * "name": "1",
     * "img": "1",
     * "create_time": "1",
     * "colour": "1",
     * "desc": "",
     * "admin_id": "1",
     * "avg_age": "",
     * "people_number": "1",
     * "status": "1"
     * },
     * "slave_team_res": {
     * "id": "1",
     * "name": "1",
     * "img": "1",
     * "create_time": "1",
     * "colour": "1",
     * "desc": "",
     * "admin_id": "1",
     * "avg_age": "",
     * "people_number": "1",
     * "status": "1"
     * }
     * }
     * }
     */
    public function getInfo()
    {

        $order_id = \Request::get('id');
        $order_res = OrderTable::ORM()->where('order_id', $order_id)->first();


        $man_team_res = [];
        $slave_team_res = [];

        if ($order_res->order_type != 3) {

            $invite = InviteTeamTable::ORM()->where('id', $order_res->object_id)->first();

            $man_team_res = TeamTable::getTeamInfo($invite->team_id);
            $slave_team_res = TeamTable::getTeamInfo($invite->join_team_id);

//            $slave_order=OrderTable::ORM()->where('order_type','!=',3)->where('object_id',$order_res->object_id)->orderBy('order_id','desc')->first();
        }

        $field_res = FieldTable::getFieldInfo($order_res->field_id);


        $status= OrderTable::status($order_res->order_type, $order_res->order_type, $order_res->pay_type, $order_res->is_comment, $order_res->status, strtotime(date('Y-m-d ', $order_res->day_time) . date('H:i:s', $order_res->end_time)));

        $data = [
            'order_type' => $order_res->order_type,
            'order_id' => $order_res->order_id,
            'storefront_name' => $field_res['storefront_name'],
            'time' => date('Y-m-d ', $order_res->day_time) . date('H:i', $order_res->start_time) . '-' . date('H:i', $order_res->end_time) . ' 周' . Str::getWeek($order_res->day_time),
            'address' => FieldTable::getFieldInfo($order_res->field_id),
            'dateline' => date('Y-m-d H:i', $order_res->dateline),
            'status' =>$status, // 翻译状态 //$order_res->status, //1 匹配中  2已匹配 3已取消
            'price' => $order_res->money,  //主队  订单金额
            'pay_type' => $order_res->pay_type,  //支付状态 1 未支付  2已支付 3已退款'
            'field_price' => $order_res->field_price,  //场地价格
            'pay_type_text' => ConstTable::PAY_TYPE_TEXT[$order_res->pay_type],
            'status_text' => ConstTable::ORDER_STATUS[$status],
            'man_team_res' => $man_team_res,
            'slave_team_res' => $slave_team_res,

        ];

        return JsonRespose::success($data);
    }

    /**
     * @api {post} /adminapi/order/cancel  取消订单
     * @apiGroup Admin-Order
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} order_id 订单
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": ''
     *
     * }
     */
    public function postCancel()
    {
        // 取消订单 测试 ing

        $order_id = \Request::get('id');
        $res = OrderTable::orderBack($order_id);
        return JsonRespose::done($res);
    }
}